//
//  WeDetailContentInfoCell.m
//  WeGalaxy
//
//  Created by Zheng on 2017/6/29.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeDetailContentInfoCell.h"

@interface WeDetailContentInfoCell () {
    BOOL _isFirst;
}

@end

@implementation WeDetailContentInfoCell

#pragma mark - 赋值
- (void)setImages:(NSArray *)images {
    if (_isFirst) {
        return;
    }
    
    _images = images;
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
        __block CGFloat totalHeight = 0;
        CGFloat x = 15;
        CGFloat y = 125;
        CGFloat width = kSCREEN_WIDTH-30;
        CGFloat bottomHeight = 55;
        
        [_images enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == 0) {
                
                UIImageView *imgv = [UIImageView new];
                
                [imgv sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil options:SDWebImageProgressiveDownload progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    CGSize size = image.size;
                    CGFloat height = size.height/size.width*width;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.contentView addSubview:imgv];
                        imgv.frame = (CGRect){x,y,width,height};
                        totalHeight += height+10;
                        dispatch_semaphore_signal(semaphore);
                    });
                }];
                
            } else if (idx == _images.count-1) {
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                
                UIImageView *imgv = [UIImageView new];
                
                [imgv sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil options:SDWebImageProgressiveDownload progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    CGSize size = image.size;
                    CGFloat height = size.height/size.width*width;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.contentView addSubview:imgv];
                        imgv.frame = (CGRect){x,y+totalHeight,width,height};
                        totalHeight += height;
                        
                        [self getTotalHeightToReloadData:totalHeight+y+bottomHeight];
                    });
                }];
                
            } else {
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                
                UIImageView *imgv = [UIImageView new];
                
                [imgv sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil options:SDWebImageProgressiveDownload progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    CGSize size = image.size;
                    CGFloat height = size.height/size.width*width;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.contentView addSubview:imgv];
                        imgv.frame = (CGRect){x,y+totalHeight,width,height};
                        totalHeight += height+10;
                        dispatch_semaphore_signal(semaphore);
                    });
                }];
            }
        }];
    });
}

#pragma mark - 获取总高度并刷新数据
- (void)getTotalHeightToReloadData:(CGFloat)totalHeight {
    _isFirst = YES;
    _totalHeightBlock(totalHeight);
}


#pragma mark - 子视图
- (void)subiewsInContentView {
    
    [self.contentView addSubview:self.titleLbl];
    [self.contentView addSubview:self.priceTitleLbl];
    [self.contentView addSubview:self.publishTimeLbl];
    [self.contentView addSubview:self.priceLbl];
    [self.contentView addSubview:self.describeLbl];
    [self.contentView addSubview:self.describeContentLbl];
    [self.contentView addSubview:self.numbersLbl];
    [self.contentView addSubview:self.lineView];
    
    [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.top.mas_equalTo(10);
    }];
    
    [_priceTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-18);
        make.top.mas_equalTo(10);
    }];
    
    [_publishTimeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.top.equalTo(_titleLbl.mas_bottom).offset(5);
    }];
    
    [_priceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.equalTo(_priceTitleLbl.mas_bottom).offset(5);
    }];
    
    [_describeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(_publishTimeLbl.mas_bottom).offset(15);
    }];
    
    [_describeContentLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.top.equalTo(_describeLbl.mas_bottom).offset(12);
    }];
    
    [_numbersLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-12);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(17);
        make.right.mas_equalTo(-17);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(_numbersLbl.mas_top).offset(-7);
    }];
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [UILabel new];
        _titleLbl.font = kCX_FONT(12);
        _titleLbl.text = @"人民大学元旦晚会求赞助";
    }
    return _titleLbl;
}

- (UILabel *)priceTitleLbl {
    if (!_priceTitleLbl) {
        _priceTitleLbl = [UILabel new];
        _priceTitleLbl.font = kCX_FONT(12);
        _priceTitleLbl.text = @"一口价";
    }
    return _priceTitleLbl;
}

- (UILabel *)publishTimeLbl {
    if (!_publishTimeLbl) {
        _publishTimeLbl = [UILabel new];
        _publishTimeLbl.font = kCX_FONT(12);
        _publishTimeLbl.text = @"发布时间2017-4-15";
    }
    return _publishTimeLbl;
}

- (UILabel *)priceLbl {
    if (!_priceLbl) {
        _priceLbl = [UILabel new];
        _priceLbl.font = kCX_FONT(12);
        _priceLbl.textColor = kRGB_COLOR(244, 66, 54, 1);
        _priceLbl.text = @"¥ 2600";
    }
    return _priceLbl;
}

- (UILabel *)describeLbl {
    if (!_describeLbl) {
        _describeLbl = [UILabel new];
        _describeLbl.font = kCX_FONT(13);
        _describeLbl.text = @"描述";
    }
    return _describeLbl;
}

- (UILabel *)describeContentLbl {
    if (!_describeContentLbl) {
        _describeContentLbl = [UILabel new];
        _describeContentLbl.font = kCX_FONT(13);
        _describeContentLbl.text = @"人民大学在3月份举报晚会，大概会有很多人参加。";
    }
    return _describeContentLbl;
}

- (UILabel *)numbersLbl {
    if (!_numbersLbl) {
        _numbersLbl = [UILabel new];
        _numbersLbl.font = kCX_FONT(10);
        _numbersLbl.textColor = [UIColor lightGrayColor];
        _numbersLbl.text = @"点赞11·游览232";
    }
    return _numbersLbl;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}


#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self subiewsInContentView];
    }
    return self;
}

#pragma mark - 绘制
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGFloat height = 67;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextSetLineWidth(context, 1);
    CGContextSetLineDash(context, 0, (CGFloat[]){3,1}, 2);
    
    CGContextMoveToPoint(context, 15, height);
    CGContextAddLineToPoint(context, (kSCREEN_WIDTH-60)/2, height);
    CGContextDrawPath(context, kCGPathStroke);
    
    CGContextMoveToPoint(context, (kSCREEN_WIDTH-60)/2+60, height);
    CGContextAddLineToPoint(context, kSCREEN_WIDTH-15, height);
    CGContextDrawPath(context, kCGPathStroke);
    
}


@end
